local limbo = status_effects.StatusEffect({name = "limbo"})
--storage stuff
------------------------------------------------------------------------
local storage = minetest.get_mod_storage()

--dust










local dead_player_inventories = {}



local function restore_inventory(playername, player)
	local inv = player:get_inventory()
	for k, v in pairs(dead_player_inventories[playername])
	do

		inv:set_list(k, v)
	end
	inv:set_list("hand", {ItemStack("wieldhand")})
	dead_player_inventories[playername] = nil
end

limbo.register_on_save(function(playername)
		local player = minetest.get_player_by_name(playername)
		restore_inventory(playername, player)
	end)

local function set_normal_physics(player)
	player:set_physics_override(
		{
			speed = 1,
			jump = 1,
			gravity = 1,
			sneak = true,
			sneak_glitch = false,
			new_move = true,
		})
end


limbo.register_on_elapsed(function(playername)
		local player = minetest.get_player_by_name(playername)
		assert(player, "Something happened")
		restore_inventory(playername, player)
		set_normal_physics(player)

	end)





local function backup_inventory(playername, player)
	if dead_player_inventories[playername]
	then
		return
	end
	local inv = player:get_inventory()
	local lists = inv:get_lists()
	dead_player_inventories[playername] = lists
end




local function set_limbo_inventory(player)
	local inv = player:get_inventory()
	inv:set_list("hand", {ItemStack("limbo_respawning:ghost_hand")})
	inv:set_list("main", {})
	inv:set_list("craft", {})
	inv:set_list("craftpreview", {})
	inv:set_list("craftresult", {})
end

local function set_limbo_movement(player)
	player:set_physics_override(
		{
			speed = 0.2,
			jump = 0.2,
			gravity = 0,
			sneak = false,
			sneak_glitch = false,
		})
end





limbo.register_on_given(function(playername, time)
		local player = minetest.get_player_by_name(playername)
		assert(player, "This shouldn't fail but it might")

		backup_inventory(playername, player)
		set_limbo_inventory(player)
		set_limbo_movement(player)



	end)

minetest.register_on_player_hpchange(
	function(player, hp_change, reason)
		local name = player:get_player_name()

		if limbo.player_has(name)
		then
			return 0
		end
		return hp_change
	end, true)

minetest.register_on_respawnplayer(function(player)
		local name = player:get_player_name()
		limbo.give_to(name, 4)
		return true
	end)

--api functions
------------------------------------------------------------------------
function limbo_respawning.get_living_players()
	local players = minetest.get_connected_players()

	local counter = 0
	local ret = {}
	for i, p in ipairs(players)
	do
		local name = p:get_player_name()
		local hp = p:get_hp()
		if not (limbo.player_has(name) or hp <= 0)
		then
			counter = counter + 1
			ret[counter] = p
		end
	end
	return ret, counter
end

function limbo_respawning.get_dead_players()
	local players = minetest.get_connected_players()

	local counter = 0
	local ret = {}
	for i, p in ipairs(players)
	do
		local name = p:get_player_name()
		local hp = p:get_hp()
		if limbo.player_has(name) or hp <= 0
		then
			counter = counter + 1
			ret[counter] = p
		end
	end
	return ret, counter
end

function limbo_respawning.return_from_limbo(player)
	local name = player:get_player_name()
	limbo.revoke(name)
end

--registering on_respawn callbacks
--registered function is called with playername as argument
local on_respawn = {}

function limbo_respawning.register_on_respawn(func)
	table.insert(on_respawn, func)
end

limbo.register_on_elapsed(function(pname)
		for i, f in ipairs(on_respawn)
		do
			f(pname)
		end
	end)
